﻿#include "stdafx.h"
#include "main_data_c.h"
#include "loadRes.h"
#include "ui/msgbox.h"

void ReadNpcName_4267B0(const char* path)
{
	mainData.cfgFileFp_7BA851C = fopen(path, "rb");
	if (!mainData.cfgFileFp_7BA851C) return;
	int num;
	while (true)
	{
		num = cfgFileParse_425030();
		if (num == 2) break;

		if (num == 0 && strcmp(mainData.fileParseStrBuf_7B785C8, "end") == 0) {
			break;
		}
		else {
			NpcIdName* npc = mainData.npcIdNames_7B786D8 + mainData.npcNum_7BA8524;
            mainData.npcNum_7BA8524++;
			npc->id = (char)mainData.fileParseFloatBuf_7B7BCE0;
			cfgFileParse_425030();
			cfgFileParse_425030();
			strcpy(npc->name, mainData.fileParseStrBuf_7B785C8);
		}
	}
	fclose(mainData.cfgFileFp_7BA851C);
}

int cfgFileParse_425030()
{
	*mainData.fileParseStrBuf_7B785C8 = 0;
	int ch = fgetc(mainData.cfgFileFp_7BA851C);
	char* pBuf;
	char numBuf[64];

	while (ch != EOF)
	{
		//Ìø¹ý×¢ÊÍÐÐ
		if (ch == '/') {
			ch = fgetc(mainData.cfgFileFp_7BA851C);
			if (ch == '/') {
				ch = fgetc(mainData.cfgFileFp_7BA851C);
				while (ch != '\n' && ch != EOF)
					ch = fgetc(mainData.cfgFileFp_7BA851C);
			}
		}
		if (ch != EOF && !isspace(ch)) {
			switch (ch)
			{
			case '#':
			case ';':
			case ',':
			case '{':
			case '}':
                mainData.fileParseTag_7B7BCDC = ch;
				return mainData.fileParseTag_7B7BCDC;
			case '"':
				ch = fgetc(mainData.cfgFileFp_7BA851C);
				pBuf = mainData.fileParseStrBuf_7B785C8;
				while (ch != '"' && ch != EOF)
				{
					*pBuf = ch;
					pBuf++;
					ch = fgetc(mainData.cfgFileFp_7BA851C);
				}
				*pBuf = 0;
                mainData.fileParseTag_7B7BCDC = 0;
				return 0;
			case '-':
			case '.':
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				pBuf = numBuf;
				do
				{
					*pBuf = ch;
					pBuf++;
					ch = fgetc(mainData.cfgFileFp_7BA851C);
				} while (isdigit(ch) || ch == '-' || ch == '.');
				*pBuf = 0;
                mainData.fileParseFloatBuf_7B7BCE0 = (float)atof(numBuf);
                mainData.fileParseTag_7B7BCDC = 1;
				return 1;
			default:
				if (isalpha(ch)) {
					pBuf = mainData.fileParseStrBuf_7B785C8;
					do
					{
						*pBuf = ch;
						pBuf++;
						ch = fgetc(mainData.cfgFileFp_7BA851C);
					} while (isalnum(ch) || ch == '.' || ch == '_');
					pBuf = 0;
                    mainData.fileParseTag_7B7BCDC = 0;
					return 0;
				}
                mainData.fileParseTag_7B7BCDC = '<';
				return '<';
				break;
			}
		}
		ch = fgetc(mainData.cfgFileFp_7BA851C);
	}

	return 2;
}

void readMuText()
{
	readText_bmd("Data\\Text.bmd");
	readMacro_txt("Data\\Macro.txt");
}

void xor3Byte_fccfab(unsigned char* pData, int nLen)
{
	static unsigned char xor_tab_fccfab[] = { 0xfc, 0xcf, 0xab };
	for (int i = 0; i < nLen; i++) pData[i] ^= xor_tab_fccfab[i % 3];
}

void readConfig()
{
	GetPrivateProfileString("LOGIN", "Version", "0.74", mainData.szVersion_54CA39C, 11, config_ini_path);
	GetPrivateProfileString("LOGIN", "SavedId", NULL, mainData.szSavedId_54CA390, 12, config_ini_path);
	GetPrivateProfileString("LOGIN", "LastIp", "127.0.0.1", cfg_lastIp, sizeof(cfg_lastIp), config_ini_path);
	cfg_lastPort = GetPrivateProfileInt("LOGIN", "LastPort",  44405, config_ini_path);

    mainData.soundOnOff_54CA688 = GetPrivateProfileInt("App", "SoundOnOff",  1, config_ini_path);
    mainData.musicOnOff_54CA4E4 = GetPrivateProfileInt("App", "MusicOnOff", 1, config_ini_path);
	windowMode_47C2CC = GetPrivateProfileInt("App", "WindowMode", 1, config_ini_path);
	int resolution = GetPrivateProfileInt("App", "Resolution", 1, config_ini_path);
	switch (resolution)
	{
	case 1:
		g_wndWidth = 800;
		g_wndHeight = 600;
		break;
	case 2:
		g_wndWidth = 1024;
		g_wndHeight = 768;
		break;
	case 3:
		g_wndWidth = 1280;
		g_wndHeight = 1024;
		break;
	default:
		g_wndWidth = 640;
		g_wndHeight = 480;
		break;
	}
}

void readText_bmd(const char* filename)
{
	FILE* fp = fopen(filename, "rb");
	if (fp) {
		size_t nRead = fread(mainData.textBmd_7B7BCE8, 180000, 1, fp);
		if (nRead == 1) {
			xor3Byte_fccfab((BYTE*)mainData.textBmd_7B7BCE8, 180000);
		}
		fclose(fp);
	}
	else {
		MsgNoFile_exit(filename, NULL);
	}
}

void readMacro_txt(const char* filename)
{
	FILE* fp = fopen(filename, "rb");
	if (fp) {
		for (size_t i = 0; i < 10; i++)
		{
			fscanf_s(fp, "%s", mainData.macroTxt_7C3FF58[i], 256);
		}
		fclose(fp);
	}
}
